package com.zenithmind.chat.controller;

import com.zenithmind.chat.service.AiChatService;
import com.zenithmind.chat.service.factory.AiServiceFactory;
import com.zenithmind.chat.temp.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查控制器
 * 遵循单一职责原则，专门负责服务健康状态检查
 */
@Slf4j
@Tag(name = "健康检查接口")
@RestController
@RequestMapping("/api/zenithMind/health")
public class HealthController {
    
    private final AiChatService aiChatService;
    private final AiServiceFactory aiServiceFactory;
    
    public HealthController(AiChatService aiChatService, AiServiceFactory aiServiceFactory) {
        this.aiChatService = aiChatService;
        this.aiServiceFactory = aiServiceFactory;
    }
    
    /**
     * 基础健康检查
     */
    @Operation(summary = "基础健康检查")
    @GetMapping("/check")
    public Result<Map<String, Object>> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", LocalDateTime.now());
        health.put("service", "ai-chat-service");
        health.put("version", "1.0.0");
        
        return Result.success(health);
    }
    
    /**
     * 详细健康检查
     */
    @Operation(summary = "详细健康检查")
    @GetMapping("/detailed")
    public Result<Map<String, Object>> detailedHealthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("timestamp", LocalDateTime.now());
        health.put("service", "ai-chat-service");
        
        try {
            // 检查AI聊天服务
            boolean aiChatServiceHealth = checkAiChatService();
            health.put("aiChatService", aiChatServiceHealth ? "UP" : "DOWN");
            
            // 检查AI服务工厂
            boolean aiServiceFactoryHealth = checkAiServiceFactory();
            health.put("aiServiceFactory", aiServiceFactoryHealth ? "UP" : "DOWN");
            
            // 整体状态
            boolean overallHealth = aiChatServiceHealth && aiServiceFactoryHealth;
            health.put("status", overallHealth ? "UP" : "DOWN");
            
            return Result.success(health);
            
        } catch (Exception e) {
            log.error("健康检查失败", e);
            health.put("status", "DOWN");
            health.put("error", e.getMessage());
            return Result.fail(500, "健康检查失败", health);
        }
    }
    
    /**
     * 检查AI聊天服务
     */
    private boolean checkAiChatService() {
        try {
            // 简单的服务可用性检查
            return aiChatService != null;
        } catch (Exception e) {
            log.warn("AI聊天服务检查失败", e);
            return false;
        }
    }
    
    /**
     * 检查AI服务工厂
     */
    private boolean checkAiServiceFactory() {
        try {
            // 检查工厂是否能正常工作
            return aiServiceFactory != null;
        } catch (Exception e) {
            log.warn("AI服务工厂检查失败", e);
            return false;
        }
    }
}
